package thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

//模拟实现线程池
class MyThreadPoll{
    private BlockingQueue<Runnable> queue = null;

    public MyThreadPoll(int n){
        queue = new ArrayBlockingQueue<>(1000);
        for (int i = 0; i < n; i++) {
            Thread t = new Thread(()->{
                while(true){
                    try {
                        Runnable task = queue.take();
                        task.run();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });

            t.start();
        }
    }

    public void submit(Runnable task) throws InterruptedException {
        queue.put(task);
    }
}

public class Demo19 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPoll poll = new MyThreadPoll(10);

        for (int i = 0; i < 100; i++) {
            int id = i;
            poll.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + " id "+id);
                }
            });
        }
    }
}
